package com.itcrowd.ittao.view;

import com.android.volley.toolbox.NetworkImageView;
import com.itcrowd.ittao.R;
import com.itcrowd.ittao.service.ShoppingListChangedListener;
import com.itcrowd.ittao.service.ShoppingListManager;
import com.itcrowd.ittao.service.data.MoneyType;
import com.itcrowd.ittao.service.data.Price;
import com.itcrowd.ittao.service.data.Product;
import com.itcrowd.ittao.utils.ImageManager;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ProductDetailActivity extends Activity implements OnClickListener, ShoppingListChangedListener {
	
	private Product mProduct;
	private Button mCancel;
	
	public void onCreate(Bundle savedInstance) {
		super.onCreate(savedInstance);
		getActionBar().setDisplayHomeAsUpEnabled(true);
		setContentView(R.layout.product_detail);
		
		int title = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
		TextView titleView = (TextView) findViewById(title);
		
		String qrCode = getIntent().getStringExtra("product");
		Product product = ShoppingListManager.getInstance().getProduct(qrCode);
		mProduct = product;
		
		titleView.setTextColor(Color.WHITE);
		getActionBar().setTitle(product.getName());
		
		NetworkImageView img = (NetworkImageView) findViewById(R.id.picture);
		ImageManager.setImageUrl(img, R.drawable.default_img_large, product.getPictureUrl());
		
		TextView price = (TextView) findViewById(R.id.price);
		price.setText(product.getPrice().toString());
		
		TextView description = (TextView) findViewById(R.id.product_info);
		description.setText(product.getName());
		
		TextView totalNumber = (TextView) findViewById(R.id.total_purchase_number);
		totalNumber.setText(String.valueOf(product.getProductStatus().getTotalNumber())+getString(R.string.item));
		
		Button purchase = (Button) findViewById(R.id.bt_purchase);
		purchase.setOnClickListener(this);
		
		mCancel = (Button) findViewById(R.id.bt_cancel);
		mCancel.setOnClickListener(this);
		
		updateCurrentProductStatus();
		ShoppingListManager.getInstance().registerObserver(this);
		
	}
	
	private void updateCurrentProductStatus() {
		TextView currentNumber = (TextView) findViewById(R.id.current_purchase_number);
		currentNumber.setText(String.valueOf(mProduct.getProductStatus().getCurrentNumber())+getString(R.string.item));
		
		TextView cost = (TextView) findViewById(R.id.cost);
		cost.setText(mProduct.getCost().toString());
		
		if(mProduct.getProductStatus().getCurrentNumber() <= 0 ) {
			mCancel.setEnabled(false);
		} else {
			mCancel.setEnabled(true);
		}
	}
	
	public void onDestroy() {
		super.onDestroy();
		ShoppingListManager.getInstance().unregisterObserver(this);
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId() == android.R.id.home) {
			finish();
		}
		return true;
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.bt_purchase:
			ShoppingListManager.getInstance().addProductToCart(mProduct.getQRCode(), 1);
			break;
		case R.id.bt_cancel:
			ShoppingListManager.getInstance().removeProductFromCart(mProduct.getQRCode(), 1);
			break;
		}
	}

	@Override
	public void onShoppingListChanged() {
		updateCurrentProductStatus();
	}

}
